package com.loie.xcx.service.impl;

import com.loie.xcx.mapper.QuotationMapper;
import com.loie.xcx.mapper.SystemMapper;
import com.loie.xcx.model.bo.IndexQueryNumBO;
import com.loie.xcx.model.bo.UserInfoAuthorityBO;
import com.loie.xcx.model.bo.UserInfoBO;
import com.loie.xcx.service.SystemService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author zhl
 */
@AllArgsConstructor
@Service
@Transactional(rollbackFor = Exception.class)
public class SystemServiceImpl implements SystemService {

    final SystemMapper systemMapper;
    final QuotationMapper quotationMapper;

    @Override
    public String getSecretByAppId(String appId) {
        return systemMapper.getSecretByAppId(appId);
    }

    @Override
    public UserInfoBO getUserInfoByPhone(String phone) {
        return systemMapper.getUserInfoByPhone(phone);
    }

    @Override
    public UserInfoBO getUserInfoByOpenId(String openId) {
        return systemMapper.getUserInfoByOpenId(openId);
    }

    @Override
    public int setUserInfoWxByOpenId(UserInfoBO userInfoBO) {
        UserInfoBO bo = systemMapper.getUserInfoByOpenId(userInfoBO.getOpenId());
        if (bo != null && StringUtils.isNotBlank(bo.getUserId())) {
            return 0;
        }
        return systemMapper.setUserInfoWxByOpenId(userInfoBO);
    }

    @Override
    public int delUserInfoWxByOpenId(String openId) {
        return systemMapper.delUserInfoWxByOpenId(openId);
    }

    @Override
    public UserInfoAuthorityBO getAuthorityInfoByOpenId(String openId) {
        return systemMapper.getAuthorityInfoByOpenId(openId);
    }

    @Override
    public IndexQueryNumBO getIndexQueryNum(String openId) {
        int completeQuotationNum = quotationMapper.getCompleteQuotationNum(openId);
        int inCompleteQuotationNum = quotationMapper.getInCompleteQuotationNum(openId);
        IndexQueryNumBO indexQueryNumBO = new IndexQueryNumBO();
        indexQueryNumBO.setIncompleteQuotationNum(inCompleteQuotationNum);
        indexQueryNumBO.setCompleteQuotationNum(completeQuotationNum);
        indexQueryNumBO.setIncompleteApprovalNum(0);
        return indexQueryNumBO;
    }
}
